function GuiUtils() {
};

GuiUtils.tasks = function(gui, user, date, data, callbackFunc, callbackFunc1, callbackFunc2, options) {
  var tr, td, popupBox = new ModulePopupBox(), _self = this;
  
  var tasks = data[0].data;
  var notes = data['note'];
  var models = data['pz_model'];
  var states = data['pz_model_state'];
  var transitions = data['pz_model_transition'];
  this._queue = data[0].queue;
  this._cond = data[0].condition;
  this._select = data['table_select'];
  this._join = data['table_join'];
  DOMUtils.removeTableRows(gui.tasks, 1);

  if (tasks.length == 0) {
    tr = gui.tasks.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 10;
    td.style.height = '23px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode('n/a'));
  }
  
  for (var i = 0, il = tasks.length; i < il; i++) {
    var task = tasks[i];
    tr = gui.tasks.insertRow(-1);
    tr.style.backgroundColor = (tr.rowIndex % 2 == 0) ? '#ececec' : '#fff';
    tr.style.color = (task.deadline && parseInt((task.deadline - date) / 86400, 10) <= 0) ? '#ff0000' : '#000000';

    td = tr.insertCell(-1);
    td.style.height = '23px';
    td.style.borderRight = 'solid 1px #bbb';
    td.style.padding = '0 5px';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((task.branch) ? task.branch : '-'));
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    
    if (task.is_wedding == 1) {
      td.style.color = 'green';
    }
    
    td.appendChild(document.createTextNode((task.model_id != 2) ? (task.customer_id != 0) ? PPublishUtils.customerId(task.customer_id) : '-' : 'Parcel ' + task.code));
    var note = document.createElement('button');
    var count = 0;
    for (var j = 0, jl = notes.length; j < jl; j++) {
      var n = notes[j];
      if (n.id == task.id) {
        count = n.value;
      }
    }
    note.className = (count == 0) ? "notebtn" : "notebtn1";
    note.style.cursor = 'pointer';
    note.style.margin = '0 0 0 5px';
    note._task = task;
    if (task.model_id == 2) {
      note.onclick = function() { div = document.createElement('div');
                                  pos = DOMUtils.findPos(this);
                                  MiscUtils.dialog(popupBox, pos, div, null, null, { t: 'Details', width: 760, height: 50 });
                                  new ModuleDialogNote(document, div, 740, 30, user, date, { task: this._task, popupBox: popupBox, callback: callbackFunc1, pos: pos });
                                  return false;
                                };
    } else {
      note.onclick = function() { var div = document.createElement('div');
                                  var pos = DOMUtils.findPos(this);
                                  var pub = (user.type == Constant.USER_TYPE_DESIGNER || user.type == Constant.USER_TYPE_MAILER || this._task.model_id != 1) ? true : false;
                                  MiscUtils.dialog(popupBox, pos, div, null, null, { t: 'Details', width: 500, height: 50 });
                                  new ModuleDialogPublish(document, div, 500, 30, user, date, { data: data, task: this._task, popupBox: popupBox, callback: callbackFunc, callback1: callbackFunc1, pos: pos, pub: pub });
                                  return false;
                                };
    }    
    td.appendChild(note);
    
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(SimpleDate.format(SimpleDate.fromStamp(task.created_date))));
    
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((task.received_date != 0) ? SimpleDate.format(SimpleDate.fromStamp(task.received_date)) : '-'));
    
    if (task.model_id == 3) {
      td = tr.insertCell(-1);
      td.style.padding = '0 5px';
      td.style.borderRight = 'solid 1px #bbb';
      if(task.is_canceled == 1){
        td.style.textDecoration = 'line-through';
      }
      td.colSpan = 2;
      var span = document.createElement('span');
      span.style.fontWeight = 'bold';
      span.appendChild(document.createTextNode('From: '));
      td.appendChild(span);
      td.appendChild(document.createTextNode((task.custom_assigner) ? task.custom_assigner : '-'));
    } else {
      td = tr.insertCell(-1);
      td.style.padding = '0 5px';
      td.style.borderRight = 'solid 1px #bbb';
      if(task.is_canceled == 1){
        td.style.textDecoration = 'line-through';
      }
      td.appendChild(document.createTextNode((task.photographer) ? task.photographer : '-'));
//      if(user.type == Constant.USER_TYPE_ADMINISTER || user.type == Constant.USER_TYPE_ASSISTANT){
//        var a = document.createElement('a');
//        a.href = '#';
//        a._task = task;
//        a.style.paddingLeft = '3px';
//        a.style.fontSize = '11px';
//        a.appendChild(document.createTextNode('Chg'));
//        a._task = task;
//        a.onclick = function() { var pos = DOMUtils.findPos(this);
//                                 var div = document.createElement('div');
//                                 var transition = this._transition;
//                                 var state = this._state;
//                                 var task = this._task;
//                                 var oldTask = MiscUtils.clone(task);
//                                 var e = null;
//                                 var t = null;
//                                 var func1 = function() { popupBox._close();
//                                                          func();
//                                                        };
//                                 var func2 = function() { popupBox._close();
//                                                          task.photogrpaher_id = oldTask.photogrpaher_id;
//                                                          task.photogrpaher = oldTask.photogrpaher;
//                                                          callbackFunc();
//                                                        };
//                                  var func = function() { new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callbackFunc, { pos: pos });
//                                                        };
//                                 MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Pick a Photographer', width: 320, height: 50 });
//                                 new ModuleDialogPickPhotographer(document, div, null, null, user, date, { object: task });
//                                 return false;
//                               };
//        if (task.is_canceled != 1 && task.state_id <15 && task.photographer && task.photographer != '') {
//          td.appendChild(a);
//        }
//      }
    
      td = tr.insertCell(-1);
      td.style.padding = '0 5px';
      td.style.borderRight = 'solid 1px #bbb';
      if(task.is_canceled == 1){
        td.style.textDecoration = 'line-through';
      }
      td.appendChild(document.createTextNode((task.photographer2) ? task.photographer2 : '-'));
//      if(user.type == Constant.USER_TYPE_ADMINISTER || user.type == Constant.USER_TYPE_ASSISTANT){
//        var a = document.createElement('a');
//        a.href = '#';
//        a._task = task;
//        a.style.paddingLeft = '3px';
//        a.style.fontSize = '11px';
//        a.appendChild(document.createTextNode('Chg'));
//        a._task = task;
//        a.onclick = function() { var pos = DOMUtils.findPos(this);
//                                 var div = document.createElement('div');
//                                 var transition = this._transition;
//                                 var state = this._state;
//                                 var task = this._task;
//                                 var oldTask = MiscUtils.clone(task);
//                                 var e = null;
//                                 var t = null;
//                                 var func1 = function() { popupBox._close();
//                                                          func();
//                                                        };
//                                 var func2 = function() { popupBox._close();
//                                                          task.photographer2_id = oldTask.photographer2_id;
//                                                          task.photographer2 = oldTask.photographer2;
//                                                          callbackFunc();
//                                                        };
//                                  var func = function() { new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callbackFunc, { pos: pos });
//                                                        };
//                                 MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Pick a Photographer', width: 320, height: 50 });
//                                 new ModuleDialogPickPhotographer(document, div, null, null, user, date, { object: task, idx: 2 });
//                                 return false;
//                               };
//        if (task.is_canceled != 1 && task.state_id <15 && task.photographer2 && task.photographer2 != '') {
//          td.appendChild(a);
//        }
//      }
    }
    
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    if (task.model_id == 3) {
      var span = document.createElement('span');
      span.style.fontWeight = 'bold';
      span.appendChild(document.createTextNode('To: '));
      td.appendChild(span);
      td.appendChild(document.createTextNode((task.custom_assignee) ? task.custom_assignee : '-'));
    } else {
      td.appendChild(document.createTextNode((task.designer) ? task.designer : '-'));
//      if(user.type == Constant.USER_TYPE_ADMINISTER || user.type == Constant.USER_TYPE_ASSISTANT) {
//        var a = document.createElement('a');
//        a.href = '#';
//        a._task = task;
//        a.style.paddingLeft = '3px';
//        a.style.fontSize = '11px';
//        a.appendChild(document.createTextNode('Chg'));
//        a._task = task;
//        a.onclick = function() { var pos = DOMUtils.findPos(this);
//                                 var div = document.createElement('div');
//                                 var transition = this._transition;
//                                 var state = this._state;
//                                 var task = this._task;
//                                 var oldTask = MiscUtils.clone(task);
//                                 var e = null;
//                                 var t = null;
//                                 var func1 = function() { popupBox._close();
//                                                          func();
//                                                        };
//                                 var func2 = function() { popupBox._close();
//                                                          task.designer_id = oldTask.designer_id;
//                                                          task.designer = oldTask.designer;
//                                                          callbackFunc();
//                                                        };
//                                  var func = function() { new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callbackFunc, { pos: pos });
//                                                        };
//                                 MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Pick a designer', width: 320, height: 50 });
//                                 new ModuleDialogPickDesigner(document, div, null, null, user, date, { object: task });
//                                 return false;
//                               };
//        if (task.is_canceled != 1 && task.state_id <15 && task.designer && task.designer != '') {
//          td.appendChild(a);
//        }
//      }
    }
    
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((PPublishUtils.object(task.state_id, states)) ? PPublishUtils.object(task.state_id, states).name : ''));
    if (task.model_id == 3) {
      td.appendChild(document.createTextNode(' (' + task.custom_task.substring(0, 30)));
      if (task.custom_task.length > 10) {
        var img = document.createElement('img');
        img.src = 'image/enlarge.png';
        img.style.cursor = 'pointer';
        img._task = task;
        img.onclick = function() { var pos = DOMUtils.findPos(this);
                                   var div = document.createElement('div');
                                   var func1 = function() { popupBox._close();
                                                          };
                                   MiscUtils.dialog(popupBox, pos, div, func1, null, {t: 'Personal Task Content', width: 500, height: 50});
                                   new ModuleDialogContent(document, div, 460, null, user, date, { object: this._task });
                                   return false;
                                 };
        td.appendChild(img);
      }
      td.appendChild(document.createTextNode(' )'));
    }
    var a1 = document.createElement('a');
    a1.href = '#';
    a1._task = task;
    a1.style.fontSize = '11px';
    a1.style.paddingLeft = '3px';
    a1.appendChild(document.createTextNode('Log'))
    a1.onclick = function() { var pos = DOMUtils.findPos(this);
                              var div = document.createElement('div');
                              var func1 = function() { popupBox._close();
                                                       callbackFunc();
                                                       if (callbackFunc2) {
                                                         callbackFunc2();
                                                       }
                                                     }
                              MiscUtils.dialog(popupBox, pos, div, func1, null, {t: 'History', width: 780, height: 50});
                              new ModuleDialogHistory(document, div, 500, 30, user, date, { object: this._task, models: models, states: states, transitions: transitions });
                              return false;
                            };
    td.appendChild(a1);
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    td.style.borderRight = 'solid 1px #bbb';
    if(task.is_canceled == 1){
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode((task.deadline) ? parseInt((task.deadline - date) / 86400, 10) : '-'));
    
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    if (task.model_id != 2) {
      ModelUtils.btns(task, models, states, transitions, td, user, date, callbackFunc, callbackFunc2);
      if (options && options.change) {
        var modify = document.createElement('img');
        modify.src = 'image/edit.png';
        modify.style.cursor = 'pointer';
        modify.style.paddingLeft = '5px';
        modify._task = task;
        modify.onclick = function() { var pos = DOMUtils.findPos(this);
                                      var div = document.createElement('div');
                                      var task = this._task;
                                      var func1 = function() { popupBox._close();
                                                               new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: []}], callbackFunc, { pos: pos });
                                                             };
                                      var func2 = function() { popupBox._close();
                                                               callbackFunc();
                                                             };
                                      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: '修改时间', width: 500, height: 50 });
                                      new ModuleDialogDeadlineChange(document, div, null, null, user, date, { object: task});
                                    };
        td.appendChild(modify);
      }
    } else {
      var a = document.createElement('a');
      a.href = '?t=receivemail&i=' + task.id;
      a.appendChild(document.createTextNode('Receive'));
      td.appendChild(a);
    }
  }
};
